package com.kevin.i18n.dynamic.controller;

import com.kevin.i18n.dynamic.configuration.MessageConstant;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequiredArgsConstructor
@Tag(name = "国际化")
@RestController
@RequestMapping("/i18n")
public class I18nController {

    private final MessageSource messageSource;

    @Operation(summary = "语言切换", description = "语言切换")
    @GetMapping
    public Object defaultF(@Parameter(name = "Accept-Language", required = true, in = ParameterIn.HEADER,
            schema = @Schema(name = "Accept-Language", allowableValues = {"zh-CN", "en-US"}))
                           @RequestHeader(name = "Accept-Language", defaultValue = "zh-CN") String lang) {
        System.err.println(lang);
        return messageSource.getMessage(MessageConstant.SUCCESS, null, LocaleContextHolder.getLocale());
    }

}

